package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.*;
import com.xinhe.web.reposity.LogisticsCompanyRepository;
import com.xinhe.web.reposity.OrderLogisticsRepository;
import com.xinhe.web.request.OrderLogisticsModel;
import com.xinhe.web.response.MemberView;
import com.xinhe.web.util.Snowflake;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@Service
public class OrderLogisticsService {
    @Autowired
    private OrderLogisticsRepository orderLogisticsRepository;
    @Autowired
    private LogisticsCompanyRepository logisticsCompanyRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    @Transactional
    public Result saveOrderLogistics(OrderLogisticsModel logisticsModel) {
        LogisticsCompany logisticsCompany = this.logisticsCompanyRepository.findById(logisticsModel.getLogisticsCompanyId()).get();
        OrderLogistics logistics = new OrderLogistics();
        BeanUtils.copyProperties(logisticsModel, logistics);
        logistics.setLogisticsCompanyName(logisticsCompany.getName());
        logistics.setLogisticsCompanyCode(logisticsCompany.getCode());
        logistics.setStatus("NOT_STARTED");
        this.orderLogisticsRepository.save(logistics);
        return Result.ok();
    }

    public QueryResults<OrderLogistics> pageOrderLogistics(int pageNum, int pageSize) {
        QOrderLogistics qOrderLogistics = QOrderLogistics.orderLogistics;
        return queryFactory.select(
                        Projections.bean(
                                OrderLogistics.class,
                                qOrderLogistics.orderId,
                                qOrderLogistics.logisticsCompanyId,
                                qOrderLogistics.orderType,
                                qOrderLogistics.logisticsInfo,
                                qOrderLogistics.logisticsCompanyCode,
                                qOrderLogistics.courierNumber,
                                qOrderLogistics.logisticsCompanyName,
                                qOrderLogistics.receiveAddress,
                                qOrderLogistics.receivePeople,
                                qOrderLogistics.receivePhone,
                                qOrderLogistics.sendAddress,
                                qOrderLogistics.sendPeople,
                                qOrderLogistics.sendPeople,
                                qOrderLogistics.status,
                                qOrderLogistics.trajectoryUrl
                        )
                ).from(qOrderLogistics)
                .offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    public Result findById(Long orderLogisticsId) {
        Optional<OrderLogistics> byId = orderLogisticsRepository.findById(orderLogisticsId);
        return Result.ok(byId.get());
    }

    public Result delete(Long orderLogisticsId) {
        orderLogisticsRepository.deleteById(orderLogisticsId);
        return Result.ok();
    }

    public Result saveModel(OrderLogisticsModel model) {
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setId(Snowflake.generateId());
        BeanUtils.copyProperties(model, orderLogistics);
        orderLogisticsRepository.save(orderLogistics);
        return Result.ok();
    }
}
